package jnpf.aop;

import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.constant.PermissionConstant;
import jnpf.util.PermissionAspectUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/15 17:12
 */
@Slf4j
@Aspect
@Component
public class OrganizeAdminIsTratorAspect {

    @Autowired
    private UserProvider userProvider;

    /**
     * 分级管理切点
     */
    @Pointcut("@annotation(jnpf.annotation.OrganizeAdminIsTrator)")
    public void pointcut() {
    }

    /**
     * 分级管理切点
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 获取用户信息
        UserInfo userInfo = userProvider.get();
        // 跳过管理员
        if (PermissionAspectUtil.getIsAdministrator(userInfo)) {
            return pjp.proceed();
        } else {
            //判断是否有当前组织的修改权限
            String organizeId = String.valueOf(pjp.getArgs()[0]);
            if (PermissionAspectUtil.containPermission(organizeId, userInfo, PermissionConstant.METHOD_UPDATE)) {
                return pjp.proceed();
            }
            return ActionResult.fail("更新失败！你没有权限操作");
        }
    }

}
